package com.cavy.platform.server.repository.impl;

import com.cavy.platform.server.domain.Pipeline;
import com.cavy.platform.server.dto.PipelineSearchDto;
import com.cavy.platform.server.repository.PipelineRepository;
import com.cavy.platform.server.repository.ProjectRepository;
import com.cavy.platform.server.util.FileUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author 唐旭
 * @Date 2018/7/18
 */
@Repository
public class PipelineRepositoryImpl implements PipelineRepository {
    public static final String PIPELINE_EXT = ".json";
    @Autowired
    private ProjectRepository projectRepository;

    @Override
    public int countByName(String workspace, String projectName, String name) {
        File pipe = getPipelineFile(workspace, projectName, name);
        if (pipe.exists()) {
            return 1;
        }
        return 0;
    }

    private File getPipelineFile(String workspace, String projectName, String name) {
        File projectDir = projectRepository.getProjectDir(workspace, projectName);
        return new File(projectDir, name + PIPELINE_EXT);
    }


    @Override
    public void create(Pipeline pipeline) {
        save(pipeline);
    }

    private void savePipeline(Pipeline pipeline) {
        File file = getPipelineFile(pipeline);
        ObjectMapper mapper = new ObjectMapper();
        try {
            mapper.writeValue(file, pipeline);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    private File getPipelineFile(Pipeline pipeline) {
        return getPipelineFile(pipeline.getProject().getWorkSpace().getName(), pipeline.getProject().getName(), pipeline.getName());
    }

    @Override
    public void delete(Pipeline pipeline) {
        File pipelineFile = getPipelineFile(pipeline);
        FileUtils.delete(pipelineFile);
    }

    @Override
    public List<Pipeline> list(PipelineSearchDto dto) {
        File projectDir = projectRepository.getProjectDir(dto.getWorkspace(), dto.getProjectName());
        File[] list;
        if (StringUtils.isEmpty(dto.getName())) {
            list = projectDir.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(PIPELINE_EXT) && !pathname.isDirectory();
                }
            });
        } else {
            list = projectDir.listFiles(new FileFilter() {
                @Override
                public boolean accept(File pathname) {
                    return pathname.getName().endsWith(PIPELINE_EXT) && !pathname.isDirectory() && pathname.getName().contains(dto.getName());
                }
            });
        }
        if (list == null || list.length == 0) {
            return Collections.emptyList();
        }
        List<Pipeline> pipelineList = new ArrayList<>(list.length);
        for (File item : list) {
            pipelineList.add(new Pipeline(item.getName().replaceAll(PIPELINE_EXT, ""), dto.getProjectName(), dto.getWorkspace()));
        }
        return pipelineList;
    }

    @Override
    public Pipeline find(String workspace, String projectName, String pipelineName) {
        File file = getPipelineFile(workspace, projectName, pipelineName);
        ObjectMapper mapper = new ObjectMapper();
        try {
            return mapper.readValue(file, Pipeline.class);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void save(Pipeline pipeline) {
        savePipeline(pipeline);
    }

}
